package com.citycloud.datac.mq.jms;

import org.apache.activemq.ActiveMQConnectionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.jms.JmsProperties;
import org.springframework.boot.autoconfigure.jms.activemq.ActiveMQProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.jms.config.DefaultJmsListenerContainerFactory;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.stereotype.Component;

import javax.jms.ConnectionFactory;

/**
 * @Description  activeMQ启动类
 * @Date 2020/1/8 9:47 AM
 * @Author liuyan
 **/
@Component
public class ActiveMQConfiguration {

    @Autowired
    private JmsProperties jmsProperties;
    @Autowired
    private ActiveMQProperties activeProperties;

    @Bean("JmsConnectionFactory")
    public ConnectionFactory connectionFactory() {
        ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory(activeProperties.getUser(), activeProperties.getPassword(), activeProperties.getBrokerUrl());
        factory.setTrustAllPackages(true);
        return factory;
    }

    @Bean("JmsMQTemplate")
    public JmsTemplate jmsTemplate() {
        JmsTemplate jmsTemplate = new JmsTemplate(connectionFactory());
        jmsTemplate.setPubSubDomain(jmsProperties.isPubSubDomain());
        return jmsTemplate;
    }

    @Bean("JmsTopicListenerContainerFactory")
    public DefaultJmsListenerContainerFactory jmsTopicListenerContainerFactory() {
        DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
        factory.setConnectionFactory(connectionFactory());
        factory.setConcurrency("1");
        factory.setPubSubDomain(true);
        return factory;
    }

}